﻿using SeaBattle.Domain.Abstract;
using SeaBattle.Domain.Entities;
using System.Collections.Generic;
using System;
using System.Linq;
using SeaBattle.Domain.Abstract.Services;


namespace SeaBattle.Domain.Concrete
{
    public class FakeGameRepository:IGameRepository
    {

        private IPlayerRepository playerRepository;
        private IShipRepository shipRepository;
        private IFleetRepository fleetRepository;

        private static List<Game> games = new List<Game>();
        private static bool gameInitialized = false;

        public FakeGameRepository(IShipRepository shipRepository, IFleetRepository fleetRepository, IPlayerRepository playerRepository)
        {
            this.shipRepository = shipRepository;
            this.fleetRepository = fleetRepository;
            this.playerRepository = playerRepository;
            if (!gameInitialized)
                InitializeGames();

        }
        
        private int GetNextId()
        {
            return games.Count + 1;
        }
        
        public Game CreateNewGame(Player creator)
        {
            return new Game(GetNextId(), DateTime.Now, creator, shipRepository, fleetRepository, playerRepository);
        }

        public bool SaveGame(Entities.Game game)
        {
            if (!games.Contains(game))
            {
                games.Add(game);
            }
            return true;
        }


        public Game GetGame(int id)
        {
            return games.FirstOrDefault(x=>x.Id==id);
        }

        public IEnumerable<Game> GetAllGames()
        {
            return from a in games select a;
        }

        private void InitializeGames()
        {
            SaveGame(new Game(1, DateTime.Now, playerRepository.GetPlayer("vasya"), shipRepository, fleetRepository, playerRepository) { Preset = 100, Opened = false });
            SaveGame(new Game(2, DateTime.Now, playerRepository.GetPlayer("andrew"), shipRepository, fleetRepository, playerRepository) { Preset = 700, Opened = true });
            SaveGame(new Game(3, DateTime.Now, playerRepository.GetPlayer("alex"), shipRepository, fleetRepository, playerRepository) { Preset = 300, Opened = true });
            SaveGame(new Game(4, DateTime.Now, playerRepository.GetPlayer("vasya"), shipRepository, fleetRepository, playerRepository) { Preset = 20, Opened = true });
            gameInitialized = true;
        }
        


    }
}
